<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
</style>
<template>
  <div class="comon">
    <div class="rj-wrap">
      {{title}}
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
        <form method="post" id="formid">
<!--          <input type="hidden" name="type" value="1" />-->
          <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">领料单编号</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" readonly :value="baseInfo.code" name="">
            </div>
          </div>
            <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">所在班组</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" readonly  type="text"  name="" :value="baseInfo.deptName">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">入库库房</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" readonly name="" :value="baseInfo.strname">
            </div>
          </div>
            <div class="weui-cell">
              <div class="weui-cell__hd"><label class="weui-label">库存组织</label></div>
              <div class="weui-cell__bd">
                <input class="weui-input" readonly  type="text"  name="" :value="baseInfo.orgname">
              </div>
            </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">创建时间</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" readonly type="text" name="" :value="baseInfo.createtime">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
            <div class="weui-cell__bd">
              <textarea class="weui-textarea" name="remark" placeholder="请输备注内容" rows="2"></textarea>
            </div>
          </div>
        </div>
          <ApplySrItem v-for="(item, index) in oitems" :key="index" :itemNo="item" :isLast="index+1 == oitems.length" :applyid="id" :types="types" @doAddItem="addItem(index + 1)" @doDelItem="delItem" @changeData="takeItem"></ApplySrItem>
          <div ref="actHtml" class="fixcont">
            <div class="weui-btn-area">
              <a class="weui-btn weui-btn_primary halfbtn subbtn"  href="javascript:" @click="send">提交</a>
              <a class="weui-btn weui-btn_primary halfbtn"  href="javascript:" @click="$router.back(-1)" id="sub">返回</a>
            </div>
          </div>
      </form>
    </div>
  </div>
</template>

<script>
  import ApplySrItem from "./ApplySrItem";
  export default {
    name: 'ApplyStoreRm2',
    data () {
      return {
        openid: localStorage.openid,
        id: '',
        baseInfo: {},
        items: [],
        specifications: [],
        product1:"",
        product2:"",
        product3:"",
        product4:"",
        product5:"",
        oitems: [0],
        nodeid: '',
        title: '入库申请',
        model: {
          items: []
        },
        types: []
      }
    },
    components: {ApplySrItem},
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
      })
    },
    mounted () {
      //提交操作定位到页面底部
      //$('#bmlogs').css('margin-bottom', this.$refs.actHtml.offsetHeight);
      let that = this;
      this.id = this.$route.query.id;
      //获取回显数据
      this.getApplyInfo(this.$route.query.id);
      this.getTypes();
    },
    computed: {
    },
    methods: {
      takeItem: function(val) {
        // if (!val) return;
        console.log('takeItem val:'. val)
        if (this.model.items.some(item => {
          return item.index == val.index;
        })) {
          this.model.items = this.model.items.map(item => {
            if (item.index == val.index) {
              return val;
            }
            return item;
          })
        } else {
          this.model.items.push(val);
        }
        console.log('takeItem this.model', this.model)
        console.log('takeItem this.model.items', this.model.items)
      },
      addItem: function(index) {
        this.oitems.push(index);
      },
      delItem: function() {
        this.oitems.pop();
        this.model.items.pop();
      },
      getApplyInfo: function(id) {
        this.axios({
          method: 'get',
          url: `/prod-api/produce/pickinglist/${id}`,
        }).then(response => {
          if (response.data.code == 200) {
            let data = response.data;
            this.baseInfo = data.data || {};
            // this.baseItems = response.data.items;
          } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
            //不不设置提示
          } else {
            this.toastError('系统错误');
          }
          this.isReady = true;
        });
      },
      getTypes: function() {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/whinapply_item_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.types = response.data.data;
          } else {
            this.toastText('获取入库类型数据失败');
          }
        });
      },
      send: function() {
        let that = this;

        if (!this.checkEmy('product')) {
          this.toastText('产品不能为空！');
          return false;
        } else if (!this.checkEmy('spec')) {
          this.toastText('规格不能为空！');
          return false;
        }

        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");
        //显示提交状态
        $.showLoading("正在提交");

        let postData = this.serialize2Obj($("#formid").serializeArray());
        postData.pickinglistid = this.baseInfo.guid;
        postData.deptId = this.baseInfo.deptId;
        postData.strmId = this.baseInfo.strm;
        postData.orgId = this.baseInfo.orgId;
        postData.deptName = this.baseInfo.deptName;
        postData.items = that.model.items;
        this.axios({
          method: 'post',
          url: '/prod-api/produce/whinapply/save',
          data: postData
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();

          if(response.data.code == 200){
            this.toastText('操作成功!');
            this.$router.push({name: 'ApplyStoreRmList2'});
          }else if (response.data.msg == 'outlogin'){
            this.toastText('请重新登录!');
          } else if (response.data.msg == 'canotopt') {
            this.toastText('不允许的操作！');
          } else if (response.data.msg == 'error'){
            this.toastError('系统错误!');
          } else if (response.data.msg){
            this.toastText(response.data.msg);
          } else {
            this.toastError('系统错误!');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();
          that.toastError('系统错误');
        });

      },
    }
  }


</script>
